﻿
using UnityEngine;

namespace GameBoxTools
{
    public class OverdrawStatistic : BaseStatistic
    {
        private Color32[] colors = new Color32[rgbaCount];
        protected override StatisticType Type
        {
            get { return StatisticType.Overdraw; }
        }

        protected override void SetMaterial()
        {
            //0x01, 0x10
            colors[0] = new Color32(0x01, 0, 0, 0);//r通道
            colors[1] = new Color32(0, 0x01, 0, 0);//g通道
            colors[2] = new Color32(0, 0, 0x01, 0);//b通道
            colors[3] = new Color32(0, 0, 0, 0x01);//a通道

            for (int i = 0; i < psr.Length; ++i)
            {
                var count = psr[i].materials.Length;
                if (count > 0)
                {
                    Material mat = new Material(Shader.Find("Custom/OverdrawShader"));
                    mat.name = string.Format("OverDraw{0}", i + 1);
                    mat.SetColor("_OverDrawColor", colors[i % rgbaCount]);//设置颜色
                    Material[] materials = new Material[count];

                    //material为None(Material)时，实际获取到的material.name为"Default-Material (Instance)"，但赋值时需要赋值为null
                    bool wrong = (psr[i].material.name == "Default-Material (Instance)");//没有主材质，需检测粒子是否有错
                    this.wrong_material[i] = wrong;

                    for (int j = 0; j < count; ++j)
                    {
                        materials[j] = mat;
                    }

                    //ref: https://docs.unity3d.com/ScriptReference/Renderer-materials.html
                    psr[i].materials = materials;
                }
            }
        }
    }
}
